﻿using ProductSystem.BLL.Common;
using ProductSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductSystem.BLL
{
    public partial class System_RoleBll
    {
        /// <summary>
        /// 分页获取符合关键字的启用系统角色
        /// </summary>
        /// <param name="Role">角色关键字</param>
        /// <param name="pageInfo">分页信息</param>
        /// <returns></returns>
        public static List<System_Role> GetList(string Role, ref PageInfo pageInfo)
        {
            List<System_Role> list = new List<System_Role>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                if (string.IsNullOrEmpty(Role))
                {
                    list = dbContext.System_Roles.Where(m => m.Status == 1).OrderByDescending(m => m.ID).ToList();
                }
                else
                {
                    list = dbContext.System_Roles.Where(m => m.Status == 1 && (m.Role_Name.Contains(Role) || m.Remark.Contains(Role))).OrderByDescending(m => m.ID).ToList();
                }
                pageInfo.TotalCount = list.Count;
                list = list.Skip((pageInfo.CurPage - 1) * pageInfo.PageSize).Take(pageInfo.PageSize).ToList();
            }
            return list;
        }

        /// <summary>
        /// 获取全部启用角色
        /// </summary>
        /// <returns></returns>
        public static List<System_Role> GetList()
        {
            List<System_Role> list = new List<System_Role>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                list = dbContext.System_Roles.Where(m => m.Status == 1).OrderByDescending(m => m.ID).ToList();
            }
            return list;
        }

        /// <summary>
        /// 判断是否存在该角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool IsExist(System_Role model)
        {
            bool flag = false;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                var item = dbContext.System_Roles.FirstOrDefault(o => o.Role_Name == model.Role_Name && o.ID != model.ID && o.Status == 1);
                if (item != null)
                {
                    flag = true;
                }
            }
            return flag;
        }

        /// <summary>
        /// 根据Id获取启用角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static System_Role GetData(int id)
        {
            using (EntitiesModel db = new EntitiesModel())
            {
                return db.System_Roles.FirstOrDefault(t => t.Status == 1 && t.ID == id);
            }
        }
    }
}
